<?php
/**
 * Created by PhpStorm.
 * User: majiawen
 * Date: 2018/2/7
 * Time: 10:35
 */

namespace app\admin\controller\v1;


use app\admin\controller\Fun;
use app\admin\service\BusinessUpdateService;
use app\admin\service\UserShaohuoService;
use app\common\Model\UserShaohuo;
use think\Db;

/**
 * @title 后台-捎货管理-内容管理-捎货类型管理-V1
 * @description 接口说明
 *
 * @header name:copyright require:1 default:v1 desc:版本号(全局)
 */
class Shaohuo extends Base
{
    /**
     * @title 可用捎货类型(下拉框使用)
     * @description
     * @author majiawen
     * @url /admin/index/index
     * @method POST
     *
     * @header name:interface require:1 default:Shaohuo desc:接口组(全局)
     * @header name:access_token type:string require:1 default: other: desc:身份验证参数,登陆时返回
     *
     * @param name:acc_id type:string require:1 default: other: desc:身份验证参数,登陆时返回
     * @param name:command type:string require:1 default: other:shaohuoType desc:可用捎货类型(下拉框使用)
     *
     * @return data:[]
     */
    public function shaohuoType()
    {

        $where = ['status' => \app\common\Model\FuncClass::STATUS_NORMAL,'is_del' => \app\common\Model\FuncClass::IS_DEL_FALSE];

        $list = Db::name('func_class a')
            ->where($where)
            ->field('fc_id id,func_name name')
            ->select();
        if ($list) {
            return Fun::success($list);
        } else {
            return Fun::error('');//TODO 无数据，定义错误类型
        }
    }

    /**
     * @title 全部捎货类型信息
     * @description
     * @author majiawen
     * @url /admin/index/index
     * @method POST
     *
     * @header name:interface require:1 default:Shaohuo desc:接口组(全局)
     * @header name:access_token type:string require:1 default: other: desc:身份验证参数,登陆时返回
     *
     * @param name:acc_id type:string require:1 default: other: desc:身份验证参数,登陆时返回
     * @param name:command type:string require:1 default:shaohuoList other:shaohuoList desc:全部捎货类型信息
     *
     * @param name:page type:string require:1 default: other: desc:页码
     * @param name:page_size type:string require:1 default: other: desc:每页个数
     * @param name:search type:string require:1 default: other: desc:查询数据
     * @param name:sort_field type:string require:1 default: other: desc:排序字段
     * @param name:sort_type type:string require:1 default: other: desc:0正序,1倒序
     *
     * @return data:[]
     */
    public function shaohuoList()
    {
//        var_dump($this->param['page']);die;
        $where['is_del'] = UserShaohuo::IS_DEL_NORMAL;
        //查询
        if (isset($this->param['search']) && $this->param['search'] != '') {
            $search = $this->param['search'];
            $where['func_name'] = ['like', '%' . $search . '%'];
        }
        $FuncClass = new \app\common\Model\FuncClass();
        if (isset($this->param['sort_field']) && $this->param['sort_field'] != '' && in_array($this->param['sort_field'], $FuncClass->sort_field) && isset($this->param['sort_type'])) {
            $order = [
                $this->param['sort_field'] => $this->sort_type[intval($this->param['sort_type'])]
            ];
        } else {
            $order = ['sort' => $this->sort_type[1]];
        }
        $list = Db::name('func_class a')
            ->where($where)
            ->order($order)
//            ->field('fc_id id,func_name name')
            ->paginate($this->page_size, false, ['page' => $this->param['page']]);
        if ($list) {
            return Fun::success(['list' => $list->items(), 'total_num' => $list->total()]);
        } else {
            return Fun::error('');//TODO 无数据，定义错误类型
        }
    }

    /**
     * @title 捎货类型 禁用,解禁,删除
     * @description
     * @author majiawen
     * @url /admin/index/index
     * @method POST
     *
     * @header name:interface require:1 default:Shaohuo desc:接口组(全局)
     * @header name:access_token type:string require:1 default: other: desc:身份验证参数,登陆时返回
     *
     * @param name:acc_id type:string require:1 default: other: desc:身份验证参数,登陆时返回
     * @param name:command type:string require:1 default:alterShaohuoStatus other:alterShaohuoStatus desc:捎货类型 禁用,解禁,删除
     *
     * @param name:ids type:string require:1 default: other:10086,10087,10088 desc:用户id
     * @param name:status type:string require:1 default: other:0 desc:用户状态:1为有效,0为禁用,''不改变
     * @param name:is_del type:string require:1 default: other:0 desc:能否提现:1为删除,''不改变
     *
     * @return data:[]
     */
    public function alterShaohuoStatus()
    {
        $ids = explode(',', trim($this->param['ids'], ','));
        $updata = [];

        //用户状态:1为有效,0为禁用
        if (isset($this->param['status']) && $this->param['status'] != '') {
            $updata['status'] = $this->param['status'] == 1 ? 1 : 0;
        }

        //能否提现:0为允许,1为不允许
        if (isset($this->param['is_del']) && $this->param['is_del'] != '') {
            $updata['is_del'] = $this->param['is_del'] == 1 ? 1 : 0;
        }
        if (count($updata) && count($ids)) {
            $FuncClass = New \app\common\Model\FuncClass();
            $res = $FuncClass->where('fc_id', 'in', $ids)
                ->update($updata);
            return Fun::success('');
        }
        return Fun::error('');//TODO 无数据，定义错误类型
    }

    /**
     * @title 捎货类型排序移动
     * @description
     * @author majiawen
     * @url /admin/index/index
     * @method POST
     *
     * @header name:interface require:1 default:Shaohuo desc:接口组(全局)
     * @header name:access_token type:string require:1 default: other: desc:身份验证参数,登陆时返回
     *
     * @param name:acc_id type:string require:1 default: other: desc:身份验证参数,登陆时返回
     * @param name:command type:string require:1 default: other:alterShaohuoSort desc:捎货类型排序移动
     *
     * @param name:ids type:string require:1 default: other:10086,10087,10088 desc:用户id
     * @param name:sort type:string require:1 default: other:0 desc:状态:1为上升,-1为下降
     *
     * @return data:[]
     */
    public function alterShaohuoSort()
    {
        $ids = explode(',', trim($this->param['ids'], ','));

        if (empty($this->param['sort'])) {
            return Fun::error('');//TODO 无数据，定义错误类型
        }
        $sort = intval($this->param['sort']) > 0 ? 1 : -1;
        $res = UserShaohuoService::alterShaohuoSortByIds($ids, $sort);

        if ($res == 1) {
            return Fun::success('');
        } elseif ($res == 2) {
            $txt = $sort == 1 ? '已经是最大值' : '已经是最小值';
            return Fun::error('802001', $txt, 1);//TODO 无数据，定义错误类型
        } else {
            return Fun::error('', '');//TODO 无数据，定义错误类型
        }
    }

    /**
     * @title 捎货类型模板插件查询
     * @description
     * @author majiawen
     * @url /admin/index/index
     * @method POST
     *
     * @header name:interface require:1 default:Shaohuo desc:接口组(全局)
     * @header name:access_token type:string require:1 default: other: desc:身份验证参数,登陆时返回
     *
     * @param name:acc_id type:string require:1 default: other: desc:身份验证参数,登陆时返回
     * @param name:command type:string require:1 default: other:WidgetList desc:捎货类型模板插件查询
     *
     * @return data:[]
     */
    public function WidgetList()
    {
        $list = Db::name('widget a')
//            ->field('fc_id id,func_name name')
            ->select();
        if ($list) {
            return Fun::success($list);
        } else {
            return Fun::error('');//TODO 无数据，定义错误类型
        }
    }

    /**
     * @title 捎货类型详情查询
     * @description
     * @author majiawen
     * @url /admin/index/index
     * @method POST
     *
     * @header name:interface require:1 default:Shaohuo desc:接口组(全局)
     * @header name:access_token type:string require:1 default: other: desc:身份验证参数,登陆时返回
     *
     * @param name:acc_id type:string require:1 default: other: desc:身份验证参数,登陆时返回
     * @param name:command type:string require:1 default: other:shaohuoDetail desc:捎货类型详情查询
     *
     * @param name:id type:string require:1 default: other:1 desc:捎货种类详情
     *
     * @return data:[]
     */
    public function shaohuoDetail()
    {
        if (empty($this->param['id']) || intval($this->param['id']) <= 0) {
            return Fun::error('');//TODO 无数据，定义错误类型
        }
        $where['is_del'] = UserShaohuo::IS_DEL_NORMAL;
        $where['fc_id'] = intval($this->param['id']);
        $detail = Db::name('func_class a')
            ->where($where)
            ->find();
        $ids = explode(',', trim($detail['widget_ids'], ','));

        $data = Db::name('widget')->where(['w_id' => ['in', $ids]])->field('w_id,widget_name,is_null,chinese_name')->select();
        $detail['widget_ids_detail'] = $data;
        if ($detail) {
            return Fun::success($detail);
        } else {
            return Fun::error('');//TODO 无数据，定义错误类型
        }
    }

    /**
     * @title 添加新的捎货类型
     * @description
     * @author majiawen
     * @url /admin/index/index
     * @method POST
     *
     * @header name:interface require:1 default:Shaohuo desc:接口组(全局)
     * @header name:access_token type:string require:1 default: other: desc:身份验证参数,登陆时返回
     *
     * @param name:acc_id type:string require:1 default: other: desc:身份验证参数,登陆时返回
     * @param name:command type:string require:1 default: other:addShaohuoType desc:添加新的捎货类型
     *
     * @param name:w_ids type:string require:1 default: other:1,2,3 desc:捎货插件id
     * @param name:name type:string require:1 default: other:10086,10087,10088 desc:捎货种类命名
     *
     * @return data:[]
     */
    public function addShaohuoType()
    {
        if (empty($this->param['w_ids']) || empty($this->param['name'])) {
            return Fun::error('');//TODO 无数据，定义错误类型
        }
        //根据acc_id查询uid
        $user = DB::name('admin_user')->where(['acc_id'=>$this->param['acc_id']])->field('uid')->find(); 
        $uid = $user['uid'];

        $w_ids = explode(',', trim($this->param['w_ids'], ','));
        $w_ids = \app\common\Model\Widget::where('w_id', 'in', $w_ids)->column('w_id');
        $w_ids = implode(',', $w_ids);
        if (empty($w_ids)) {
            return Fun::error('');//TODO 无数据，定义错误类型
        }
        $sort = \app\common\Model\FuncClass::max('sort');

        $data = [
            'func_name' => $this->param['name'],
            'widget_ids' => $w_ids,
            'sort' => $sort + 1,
            'status' => 0,
            'create_uid' => $uid,
            'is_star' => 0,
        ];
        $res = Db::name('func_class')
                ->insert($data);
                
        BusinessUpdateService::shaohuoBusinessUpdateLog($uid);       
        if ($res) {
            return Fun::success('添加成功');
        }else{
            return Fun::error('333332');
        }                        

    }   

    /**
     * @title 编辑捎货类型
     * @description
     * @author majiawen
     * @url /admin/index/index
     * @method POST
     *
     * @header name:interface require:1 default:Shaohuo desc:接口组(全局)
     * @header name:access_token type:string require:1 default: other: desc:身份验证参数,登陆时返回
     *
     * @param name:acc_id type:string require:1 default: other: desc:身份验证参数,登陆时返回
     * @param name:command type:string require:1 default: other:editShaohuoType desc:编辑捎货类型
     *
     * @param name:fc_id type:string require:1 default: other:1 desc:类型id
     * @param name:w_ids type:string require:1 default: other:1,2,3 desc:捎货插件id
     * @param name:name type:string require:1 default: other:10086,10087,10088 desc:捎货种类命名
     *
     * @return data:[]
     */
    public function editShaohuoType()
    {
        if (empty($this->param['fc_id']) || empty($this->param['w_ids']) || empty($this->param['name'])) {
            return Fun::error('');//TODO 无数据，定义错误类型
        }
        //根据acc_id查询uid
        $user = DB::name('admin_user')->where(['acc_id'=>$this->param['acc_id']])->field('uid')->find(); 
        $uid = $user['uid'];

        $w_ids = explode(',', trim($this->param['w_ids'], ','));
        $w_ids = \app\common\Model\Widget::where('w_id', 'in', $w_ids)->column('w_id');
        $w_ids = implode(',', $w_ids);
        if (empty($w_ids)) {
            return Fun::error('333332');//TODO 无数据，定义错误类型
        }
        $FuncClass = new \app\common\Model\FuncClass();
        $res = $FuncClass->where('fc_id', $this->param['fc_id'])->find();
        if (!$res) {
            return Fun::error('801008');   
        }
        // 启动事务
        Db::startTrans();
        try {
            $res->func_name = $this->param['name'];
            $res->widget_ids = $w_ids;
            $res->update_uid = $uid;
            $res->save();
            // 提交事务
            Db::commit();
            return Fun::success('添加成功');
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return Fun::error($e->getMessage());//TODO 无数据，定义错误类型
        }   
    }
}